package com.dawn.module.mp.controller.admin.msgtemplatelog;

import com.dawn.module.mp.service.msgtemplate.MsgTemplateService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.dawn.framework.common.pojo.PageParam;
import com.dawn.framework.common.pojo.PageResult;
import com.dawn.framework.common.pojo.CommonResult;
import com.dawn.framework.common.util.object.BeanUtils;
import static com.dawn.framework.common.pojo.CommonResult.success;

import com.dawn.framework.excel.core.util.ExcelUtils;

import com.dawn.framework.apilog.core.annotation.ApiAccessLog;
import static com.dawn.framework.apilog.core.enums.OperateTypeEnum.*;

import com.dawn.module.mp.controller.admin.msgtemplatelog.vo.*;
import com.dawn.module.mp.dal.dataobject.msgtemplatelog.MsgTemplateLogDO;
import com.dawn.module.mp.service.msgtemplatelog.MsgTemplateLogService;

@Tag(name = "管理后台 - 模板消息记录")
@RestController
@RequestMapping("/mp/msg-template-log")
@Validated
public class MsgTemplateLogController {

    @Resource
    private MsgTemplateLogService msgTemplateLogService;


    @Resource
    private MsgTemplateService msgTemplateService;

    @PostMapping("/create")
    @Operation(summary = "创建模板消息记录")
    @PreAuthorize("@ss.hasPermission('mp:msg-template-log:create')")
    public CommonResult<Long> createMsgTemplateLog(@Valid @RequestBody MsgTemplateLogSaveReqVO createReqVO) {
        return success(msgTemplateLogService.createMsgTemplateLog(createReqVO));
    }

    @PostMapping("/update")
    @Operation(summary = "更新模板消息记录")
    @PreAuthorize("@ss.hasPermission('mp:msg-template-log:update')")
    public CommonResult<Boolean> updateMsgTemplateLog(@Valid @RequestBody MsgTemplateLogSaveReqVO updateReqVO) {
        msgTemplateLogService.updateMsgTemplateLog(updateReqVO);
        return success(true);
    }

    @GetMapping("/delete")
    @Operation(summary = "删除模板消息记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mp:msg-template-log:delete')")
    public CommonResult<Boolean> deleteMsgTemplateLog(@RequestParam("id") Long id) {
        msgTemplateLogService.deleteMsgTemplateLog(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得模板消息记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mp:msg-template-log:query')")
    public CommonResult<MsgTemplateLogRespVO> getMsgTemplateLog(@RequestParam("id") Long id) {
        MsgTemplateLogDO msgTemplateLog = msgTemplateLogService.getMsgTemplateLog(id);
        return success(BeanUtils.toBean(msgTemplateLog, MsgTemplateLogRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得模板消息记录分页")
    @PreAuthorize("@ss.hasPermission('mp:msg-template-log:query')")
    public CommonResult<PageResult<MsgTemplateLogRespVO>> getMsgTemplateLogPage(@Valid MsgTemplateLogPageReqVO pageReqVO) {
        PageResult<MsgTemplateLogDO> pageResult = msgTemplateLogService.getMsgTemplateLogPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MsgTemplateLogRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出模板消息记录 Excel")
    @PreAuthorize("@ss.hasPermission('mp:msg-template-log:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMsgTemplateLogExcel(@Valid MsgTemplateLogPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MsgTemplateLogDO> list = msgTemplateLogService.getMsgTemplateLogPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "模板消息记录.xls", "数据", MsgTemplateLogRespVO.class,
                        BeanUtils.toBean(list, MsgTemplateLogRespVO.class));
    }


    /**
     * 重新发送
     */
    @PostMapping("/send")
    @Operation(summary = "重新发送模板消息")
    @PreAuthorize("@ss.hasPermission('mp:msg-template:send')")
    public CommonResult syncWxTemplate(@RequestParam("id") Long id)  {
        MsgTemplateLogDO msgTemplateLog = msgTemplateLogService.getMsgTemplateLog(id);
        msgTemplateService.repeteSendTemplateMsg(msgTemplateLog);
        return CommonResult.success(true);
    }

}
